<template>
  <a-modal
    title="操作"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
  >
    <a-form :form="form">

      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="新词"
      >
        <a-input
          v-decorator="['word',{rules: [{ required: true, message: '新词不能为空' }]}]"
          placeholder="请输入新词"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { newWordAdd, newWordEdit } from '@/api/system'
export default {
  name: 'NewWordModal',
  components: {
  },
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      method: 'add',
      mdl: {},
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  beforeCreate () {
  },
  created () {
  },
  methods: {
    add (parentId) {
      this.method = 'add'
      this.form.resetFields()
      this.visible = true
    },
    edit (record) {
      this.method = 'edit'
      if (record.id) {
        this.mdl = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue({ 'word': this.mdl.rootWord })
        })
      }
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true
          if (this.method === 'add') {
            newWordAdd(values).then(res => {
                if (res.status === 1) {
                  this.$message.success(res.message)
                  this.$emit('ok')
                  this.visible = false
                } else {
                  this.$message.error(res.message)
                }
              }).finally(() => {
                this.confirmLoading = false
              })
            } else {
              newWordEdit(this.mdl.id, values).then(res => {
                  if (res.status === 1) {
                    this.$message.success(res.message)
                    this.$emit('ok')
                    this.visible = false
                  } else {
                    this.$message.error(res.message)
                  }
                }).finally(() => {
                  this.confirmLoading = false
                })
              }
          }
      })
    }
  },
  watch: {
  }
}
</script>

<style scoped>
  .ant-form-item-children .ant-input-number {
    width: 100%
  }
</style>
